import type { EChartsOption } from 'echarts';
//import { reflectOptions } from '/@/views/thingsborad/dashboards/components/ConfigureDashboard/components/modalClassify/chartModal/chartModal.data';
export function publicChartOptions(formData: Recordable): EChartsOption {
  const { dataZoom, _theme, tooltip, grid } = formData;
  return {
    grid: {
      left: `${grid.left}%`,
      right: `${grid.right}%`,
      bottom: `${grid.bottom}%`,
      top: `${grid.top}%`,
      containLabel: true,
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
      },
      ...tooltip,
    },
    dataZoom,
    xAxis: {
      type: 'time',
      // minInterval: 3600 * 1000 * 24,
      // maxInterval: 3600 * 1000 * 24,
      // boundaryGap: false,
      // axisLabel: {
      //   formatter: {
      //     year: '{yyyy}',
      //     month: '{MMM}',
      //     day: '{d}',
      //     hour: '{HH}:{mm}',
      //     minute: '{HH}:{mm}',
      //     second: '{HH}:{mm}:{ss}',
      //     millisecond: '{hh}:{mm}:{ss} {SSS}',
      //     none: '{yyy}-{MM}-{dd} {hh}:{mm}:{ss} {SSS}',
      //   },
      // },
    },
    yAxis: {
      type: 'value',
      splitLine: {
        show: true,
        lineStyle: {
          type: 'dashed',
        },
      },
    },
  };
}
// 设置示例数据
export function setChartExampleData(formData: Recordable) {
  let options: EChartsOption = {};
  if (formData.chartType == 'line') {
    options = {
      ...publicChartOptions(formData),
      legend: {
        orient: formData.legend.orient || 'horizontal',
        type: 'scroll',
        left: `${formData.legend.left}%`,
        top: `${formData.legend.top}%`,
        /* right: `${formData.legend.right}%`,
        bottom: `${formData.legend.bottom}%`, */
        itemWidth: 10,
        itemHeight: 2,
        data: [
          {
            name: '运行设备数',
            itemStyle: { opacity: 0 },
          },
        ],
      },
      series: [
        {
          name: '运行设备数',
          data: [],
          type: formData.chartType,
          symbol: 'none',
          itemStyle: {
            color: '#126FE1',
          },
        },
      ],
    };
  } else if (formData.chartType == 'bar') {
    options = {
      ...publicChartOptions(formData),
      legend: {
        orient: formData.legend.orient || 'horizontal',
        type: 'scroll',
        left: `${formData.legend.left}%`,
        // right: `${formData.legend.right}%`,
        // bottom: `${formData.legend.bottom}%`,
        top: `${formData.legend.top}%`,
        itemWidth: 10,
        itemHeight: 10,
        data: [
          {
            name: '运行设备数',
          },
          {
            name: '故障设备数',
          },
        ],
      },
      series: [
        {
          name: '运行设备数',
          data: [
            [1731369600000, 40],
            [1731373200000, 45],
            [1731376800000, 30],
            [1731380400000, 35],
            [1731384000000, 45],
            [1731387600000, 45],
            [1731391200000, 55],
            [1731394800000, 45],
            [1731398400000, 46],
            [1731402000000, 55],
            [1731405600000, 49],
            [1731409200000, 42],
            [1731412800000, 38],
            [1731416400000, 37],
            [1731420000000, 39],
            [1731423600000, 38],
          ],
          type: formData.chartType,
          itemStyle: {
            color: '#00C774',
          },
        },
        {
          name: '故障设备数',
          data: [
            [1731369600000, 10],
            [1731373200000, 12],
            [1731376800000, 13],
            [1731380400000, 13],
            [1731384000000, 13],
            [1731387600000, 14],
            [1731391200000, 14],
            [1731394800000, 14],
            [1731398400000, 18],
            [1731402000000, 18],
            [1731405600000, 16],
            [1731409200000, 16],
            [1731412800000, 16],
            [1731416400000, 15],
            [1731420000000, 14],
            [1731423600000, 14],
          ],
          type: formData.chartType,
          itemStyle: {
            color: '#FFCF33',
          },
        },
      ],
    };
  } else if (formData.chartType == 'lineBar') {
    options = {
      ...publicChartOptions(formData),
      legend: {
        orient: formData.legend.orient || 'horizontal',
        type: 'scroll',
        left: `${formData.legend.left}%`,
        top: `${formData.legend.top}%`,
        bottom: '10%',
        itemWidth: 10,
        itemHeight: 10,
        data: [
          {
            name: '运行设备数',
          },
          {
            name: '故障设备数',
            itemStyle: { opacity: 0 },
          },
        ],
      },
      yAxis: [
        {
          type: 'value',
          name: '运行设备数(台)',
          position: 'left',
          alignTicks: true,
          axisLine: {
            show: true,
            lineStyle: {
              // color: '#FFCF33',
            },
          },
          axisLabel: {
            formatter: '{value}',
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed',
            },
          },
        },
        {
          type: 'value',
          name: '故障设备数(台)',
          position: 'right',
          alignTicks: true,
          axisLine: {
            show: true,
            lineStyle: {
              // color: '#00C774',
            },
          },
          axisLabel: {
            formatter: '{value}',
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed',
            },
          },
        },
      ],
      series: [
        {
          name: '故障设备数',
          type: 'line',
          color: '#FFCF33',
          yAxisIndex: 1,
          // itemStyle: { opacity: 0 },
          data: [
            [1731355200000, 2],
            [1731358800000, 2],
            [1731362400000, 3],
            [1731366000000, 5],
            [1731369600000, 7],
            [1731373200000, 8],
            [1731376800000, 6],
            [1731380400000, 6],
            [1731384000000, 5],
            [1731387600000, 9],
            [1731391200000, 9],
            [1731394800000, 10],
            [1731398400000, 10],
            [1731402000000, 9],
            [1731405600000, 8],
            [1731409200000, 9],
            [1731412800000, 7],
            [1731416400000, 7],
            [1731420000000, 7],
            [1731423600000, 5],
          ],
        },
        {
          name: '运行设备数',
          type: 'bar',
          color: '#00C774',
          yAxisIndex: 0,
          data: [
            [1731355200000, 38],
            [1731358800000, 38],
            [1731362400000, 39],
            [1731366000000, 38],
            [1731369600000, 40],
            [1731373200000, 45],
            [1731376800000, 30],
            [1731380400000, 35],
            [1731384000000, 45],
            [1731387600000, 45],
            [1731391200000, 55],
            [1731394800000, 45],
            [1731398400000, 46],
            [1731402000000, 55],
            [1731405600000, 49],
            [1731409200000, 42],
            [1731412800000, 38],
            [1731416400000, 37],
            [1731420000000, 39],
            [1731423600000, 38],
          ],
        },
      ],
    };
  } else if (formData.chartType == 'pie') {
    const { legend, name, chartType, radius, tooltip, emphasis } = formData;
    options = {
      tooltip: {
        /* backgroundColor: tooltip.backgroundColor,
        borderColor: tooltip.borderColor,
        borderWidth: tooltip.borderWidth,
        textStyle: {
          color: tooltip.textStyle.color,
          fontSize: tooltip.textStyle.fontSize,
          fontWeight: tooltip.textStyle.fontWeight,
        }, */
        trigger: 'item',
        ...tooltip,
      },
      legend: {
        /* top: '5%',
        left: 'center', */
        left: `${legend.left ? legend.left + '%' : 'center'}`,
        top: `${legend.top}%`,
        itemGap: 20,
        itemWidth: 12,
        itemHeight: 12,
        textStyle: {
          color: 'rgba(0, 0, 0, 0.65)',
          fontSize: 14,
          fontStyle: 'normal',
          fontWeight: 'normal',
        },
      },
      series: [
        {
          name: name,
          type: chartType,
          // radius: `${radius}%`,
          radius: [`${radius.innerRadius}%`, `${radius.outRadius}%`],
          avoidLabelOverlap: false,
          label: {
            show: false,
            position: 'center',
          },
          emphasis: {
            label: {
              show: true,
              fontSize: 40,
              fontWeight: 'bold',
            },
            itemStyle: {
              shadowOffsetX: 0,
              ...(emphasis
                ? {
                    shadowBlur: emphasis.itemStyle.shadowBlur,
                    shadowColor: emphasis.itemStyle.shadowColor,
                  }
                : ''),
            },
          },
          labelLine: {
            show: false,
          },
          data: [
            { value: 1048, name: '设备1', itemStyle: { color: '#5B8FF9' } },
            { value: 735, name: '设备2', itemStyle: { color: '#CDDDFD' } },
            { value: 580, name: '设备3', itemStyle: { color: '#62DDAA' } },
            { value: 484, name: '设备4', itemStyle: { color: '#9661BC' } },
            { value: 300, name: '设备5', itemStyle: { color: '#78D3F8' } },
          ],
        },
      ],
    };
  } else if (formData.chartType == 'gauge') {
    const { barOne, barTwo, data } = formData;
    options = {
      // backgroundColor: '#011C2F',
      tooltip: {
        trigger: 'item',
      },
      angleAxis: {
        show: false,
        max: (100 * 360) / 270,
        type: 'value',
        startAngle: 225,
        splitLine: {
          show: false,
        },
      },
      //圆环宽度
      barMaxWidth: 20,
      radiusAxis: {
        show: false,
        type: 'category',
        z: 10,
      },
      polar: {
        //圆环大小
        radius: '160%',
      },
      series: [
        {
          type: 'bar',
          data: [
            {
              value: data,
              itemStyle: {
                color: barOne.itemStyle.color,
                // shadowColor: 'rgba(35, 176, 176, 0.5)',
                // shadowBlur: 10,
              },
            },
          ],
          barGap: '-100%',
          coordinateSystem: 'polar',
          roundCap: true,
          z: 2,
          animationDuration: 2000,
        },
        {
          //底层圆环
          type: 'bar',
          data: [
            {
              value: 100,
              itemStyle: {
                color: barTwo.itemStyle.color,
              },
            },
          ],
          barGap: '-100%',
          coordinateSystem: 'polar',
          roundCap: true,
          z: 1,
          animation: false,
        },
        {
          name: '',
          type: 'gauge',
          //startAngle: 180,
          //endAngle: 0,
          min: 0,
          max: 100,
          splitNumber: 10,
          radius: '70%', //图表尺寸
          center: ['50%', '50%'],
          detail: {
            formatter: '{r1|{value}}{r2|%}',
            color: 'auto',
            rich: {
              r1: {
                fontSize: 30,
                verticalAlign: 'bottom',
                padding: [0, 5, -2, 0],
              },
              r2: {
                fontSize: 20,
                verticalAlign: 'bottom',
              },
            },
          },
          anchor: {
            show: true,
            showAbove: true,
            size: 10,
            itemStyle: {
              borderWidth: 2,
              borderColor: '#2262E4',
            },
          },
          pointer: {
            icon: 'path://M2090.36389,615.30999 L2090.36389,615.30999 C2091.48372,615.30999 2092.40383,616.194028 2092.44859,617.312956 L2096.90698,728.755929 C2097.05155,732.369577 2094.2393,735.416212 2090.62566,735.56078 C2090.53845,735.564269 2090.45117,735.566014 2090.36389,735.566014 L2090.36389,735.566014 C2086.74736,735.566014 2083.81557,732.63423 2083.81557,729.017692 C2083.81557,728.930412 2083.81732,728.84314 2083.82081,728.755929 L2088.2792,617.312956 C2088.32396,616.194028 2089.24407,615.30999 2090.36389,615.30999 Z',
            length: '75%',
            width: 10,
            offsetCenter: [0, '5%'],
          },
          progress: {
            roundCap: true,
            show: true,
            width: 0,
          },
          itemStyle: {
            color: '#2262E4',
          },
          data: [
            {
              value: data,
              name: '',
            },
          ],
          axisLine: {
            show: true,
            lineStyle: {
              width: 0,
              shadowBlur: 0,
              color: [
                [0.2, '#23AFAF'],
                [0.4, '#2270DA'],
                [0.6, '#E99D02'],
                [1, '#F45656'],
              ],
            },
          },
          axisTick: {
            show: true,
            lineStyle: {
              color: 'auto',
              width: 2,
            },
            length: 10,
            splitNumber: 5,
          },
          splitLine: {
            show: true,
            length: 12,
            lineStyle: {
              color: 'auto',
              width: 2,
            },
          },
          axisLabel: {
            distance: 5,
            color: '#9BA5BC',
            fontSize: 12,
          },
        },
      ],
    };
  } else if (formData.chartType == 'map') {
    const { tooltip, itemStyle, emphasis, roam, zoom, label } = formData;
    options = {
      tooltip: {
        show: tooltip.show,
        backgroundColor: '#fff',
        // borderColor: '#ff2b73',
        triggerOn: 'mousemove', //click
        enterable: true,
      },
      roam: roam,
      series: [
        {
          name: 'china',
          type: formData.chartType,
          map: 'china',
          colorBy: 'series',
          label: {
            show: label.show,
            color: 'rgba(0, 0, 0, 0.85)',
          },
          itemStyle: {
            areaColor: itemStyle.areaColor,
            borderColor: '#000',
            /* shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
            shadowBlur: 10, */
          },
          emphasis: {
            disabled: false,
            label: {
              fontWeight: 'bold',
              fontSize: 16,
              color: '#000',
            },
            itemStyle: {
              areaColor: emphasis.itemStyle.areaColor,
              borderColor: '#000',
              // shadowOffsetX: 0,
              // shadowColor: 'rgba(0, 0, 0, 0.5)',
              // shadowBlur: 10,
            },
          }, //鼠标悬浮效果
          zoom: zoom,
        },
      ],
    };
  } else if (formData.chartType == 'liquidFill') {
    const { totalValue, currentValue } = formData;
    const data = [currentValue / totalValue, totalValue];
    const color = ['#f9dfa9', '#f7cb71', '#faae18'];
    options = {
      title: {
        text: '水球',
        bottom: '2%',
        left: 'center',
      },
      graphic: [
        // 扩展自定义文字
        {
          type: 'text',
          top: '50%',
          left: 'center',
          z: 2,
          // bottom:  -80,
          style: {
            text: data[0] * 100 + '%',
            fill: '#000',
            fontSize: 32,
            fontWeight: 'bold',
          },
        },
        {
          type: 'text',
          left: 'center',
          z: 2,
          top: '45%',
          style: {
            text: data[1] + '台',
            fill: '#000',
            fontSize: 14,
          },
        },
      ],
      series: [
        {
          type: formData.chartType,
          // 数据值，0 到 1 之间的数值
          data: [data[0], data[0], data[0]],
          shape: 'circle', // 形状，默认为圆形
          radius: '80%', // 半径大小
          center: ['50%', '50%'], // 圆心位置
          color: color, // 颜色
          amplitude: 20, //波段幅度
          outline: {
            // 最外层圆
            show: true,
            borderDistance: 10,
            itemStyle: {
              borderColor: color[2],
              borderWidth: 1,
            },
          },
          backgroundStyle: {
            color: '#fff',
          },
          itemStyle: {
            normal: {
              shadowBlur: 0,
            },
          },
          label: {
            normal: {
              show: false,
            },
          },
        },
      ],
    };
  }
  return {
    options,
    chartType: formData.chartType,
  };
}

// ws第一次发送消息的处理函数
export const processingChartPublicFormData = (cmdId: number, formData: Recordable) => {
  const sendMsg: Recordable = {
    cmdId,
    query: {
      pageLink: {
        pageSize: 1024,
        page: 0,
        sortOrder: {
          key: {
            type: 'ENTITY_FIELD',
            key: 'createdTime',
          },
          direction: 'DESC',
        },
      },
      entityFields: [
        {
          type: 'ENTITY_FIELD',
          key: 'name',
        },
        {
          type: 'ENTITY_FIELD',
          key: 'label',
        },
        {
          type: 'ENTITY_FIELD',
          key: 'additionalInfo',
        },
      ],
      latestValues: [],
      entityFilter: {
        type: 'singleEntity',
        singleEntity: JSON.parse(formData.deviceId),
      },
    },
  };
  if (formData.chartType === 'line' || formData.chartType == 'bar') {
    sendMsg.query.latestValues = [];
  } else if (formData.chartType == 'pie') {
    formData.series.forEach((item: Recordable) => {
      sendMsg.query.latestValues.push(JSON.parse(item.key));
    });
  }

  return {
    entityDataCmds: [sendMsg],
  };
};
// ws第二次发送消息的处理函数
export const processingChartPrivateFormData = (cmdId: number, formData: Recordable) => {
  // const keys: string[] = [];
  let sendMsg: Recordable = {};
  if (formData.chartType === 'line' || formData.chartType == 'bar') {
    /* formData.series.forEach((item: Recordable) => {
      keys.push(JSON.parse(item.key).key);
    });
    const { agg, limit, times, interval } = formData;
    const endTs = new Date().getTime();
    const startTs = endTs - reflectOptions[times].startTs;
    sendMsg = {
      cmdId,
      historyCmd: {
        keys,
        startTs,
        endTs,
        interval,
        limit: !!limit ? limit : reflectOptions[times].startTs / interval,
        agg,
      },
    }; */
    sendMsg = {
      cmdId,
      tsCmd: {
        agg: 'AVG',
        interval: 1000,
        keys: ['temperature'],
        limit: 61,
        startTs: 1742266899000,
        timeWindow: 61000,
      },
    };
  }
  return {
    entityDataCmds: [sendMsg],
  };
};

// ws第一次获得数据的处理函数
export const processingChartPublicReceiveData = (data: Recordable, formData: Recordable) => {
  let options: EChartsOption = {};
  const { name, chartType, radius, emphasis, tooltip } = formData;

  options = {
    tooltip: {
      // backgroundColor: tooltip.backgroundColor,
      // borderColor: tooltip.borderColor,
      // borderWidth: tooltip.borderWidth,
      // textStyle: {
      //   color: tooltip.textStyle.color,
      //   fontSize: tooltip.textStyle.fontSize,
      //   fontWeight: tooltip.textStyle.fontWeight,
      // },
      ...tooltip,
    },
    series: [
      {
        name: name,
        type: chartType,
        // radius: `${radius}%`,
        radius: [`${radius.innerRadius}%`, `${radius.outRadius}%`],
        data: [
          { value: 1048, name: '示例1' },
          { value: 735, name: '示例2' },
          { value: 580, name: '示例3' },
          { value: 484, name: '示例4' },
          { value: 300, name: '示例5' },
        ],
        emphasis: {
          itemStyle: {
            shadowOffsetX: 0,
            ...(emphasis
              ? {
                  shadowBlur: emphasis.itemStyle.shadowBlur,
                  shadowColor: emphasis.itemStyle.shadowColor,
                }
              : ''),
          },
        },
      },
    ],
  };
  return { options, title: formData.cardTitle };
};
// ws第二次获得数据的处理函数
export const processingChartPrivateReceiveData = (data: Recordable, formData: Recordable) => {
  let legendKeys: string[] = [];
  const series: Recordable[] = [];
  let options: EChartsOption = {};
  if (formData.chartType == 'line' || formData.chartType == 'bar') {
    if (data.update) {
      // 图例数组
      legendKeys = Object.keys(data.update[0].timeseries);
      // 图例对应数据
      formData.series.forEach((item: Recordable) => {
        const seriesData: string[][] = [];
        data.update[0].timeseries[JSON.parse(item.key).key].forEach((item: Recordable) => {
          seriesData.push(Object.values(item));
        });
        series.push({
          name: JSON.parse(item.key).key,
          data: seriesData,
          type: formData.chartType,
          itemStyle: {
            color: item.color,
          },
        });
      });
    }
    options = {
      ...publicChartOptions(formData),
      legend: {
        orient: formData.legend.orient,
        type: 'scroll',
        left: `${formData.legend.left}%`,
        top: `${formData.legend.top}%`,
        data: legendKeys,
      },
      series,
    };
  } else if (formData.chartType == 'pie') {
    if (data.data) {
    }
  }
  return { options, title: formData.cardTitle };
};
